package com.seocoo.worker.order;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jaeger.library.StatusBarUtil;
import com.seocoo.worker.R;
import com.seocoo.worker.order.model.OrderModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMFragment;

import java.util.ArrayList;
import java.util.Objects;

/**
 * author : Ssp
 * date   : 2020/2/23 16:25
 * desc   : 【测量工匠 订单主界面】
 */
public class OrderHomeGoldFragment extends MVVMFragment {
    private TextView tv_title;
    private final String[] mTitles = {"全部", "待接单", "测量进行中", "待付款", "待取货", "待安装", "待评价"};

    @Override
    public int getLayout() {
        return R.layout.order_home_gold_fragment;
    }

    @Override
    public void initData(View view) {
        tv_title = view.findViewById(R.id.tv_title);
        StatusBarUtil.setTransparentForImageViewInFragment(getActivity(), tv_title);
        SlidingTabLayout tab_layout = view.findViewById(R.id.tab_layout);
        ViewPager viewpager = view.findViewById(R.id.viwepager);
        ArrayList<Fragment> list = new ArrayList<>();
        OrderGoldFragment fragment0 = OrderGoldFragment.newInstance("0");
        list.add(fragment0);
        OrderGoldFragment fragment1 = OrderGoldFragment.newInstance("1");
        list.add(fragment1);
        OrderGoldFragment fragment2 = OrderGoldFragment.newInstance("2");
        list.add(fragment2);
        OrderGoldFragment fragment3 = OrderGoldFragment.newInstance("3");
        list.add(fragment3);
        OrderGoldFragment fragment4 = OrderGoldFragment.newInstance("4");
        list.add(fragment4);
        OrderGoldFragment fragment5 = OrderGoldFragment.newInstance("5");
        list.add(fragment5);
        OrderGoldFragment fragment6 = OrderGoldFragment.newInstance("6");
        list.add(fragment6);
        tab_layout.setViewPager(viewpager, mTitles, Objects.requireNonNull(getActivity()), list);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshData(position, list);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tab_layout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                refreshData(position, list);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        tv_title.setOnClickListener(v -> {
            String selectTxt = tv_title.getText().toString();
            new StatusDialog(getActivity(), selectTxt, (dialog, item) -> {
                dialog.dismiss();
                //takeState:1-接单中 2-休息
                if (TextUtils.equals("接单中", item)) {
                    getViewModel(OrderModel.class).changeTakeState("1");
                } else if (TextUtils.equals("休息", item)) {
                    getViewModel(OrderModel.class).changeTakeState("2");
                }

            }).show(v);
        });
        getViewModel(OrderModel.class).queryTakeState();
    }

    private void refreshData(int position, ArrayList<Fragment> list) {
        OrderGoldFragment fragment = (OrderGoldFragment) list.get(position);
        if (fragment != null) {
            fragment.reFreshData();
        }
    }

    @LiveDataMatch
    public void onChangeTakeState(String takeState) {
        if (TextUtils.equals("1", takeState)) {
            tv_title.setText("接单中");
        } else if (TextUtils.equals("2", takeState)) {
            tv_title.setText("休息");
        }
    }
}
